package origin.database;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

import origin.util.Config;
import origin.util.Constants;

public class DBCPConnectionPool implements IConnectionPool {

	private BasicDataSource dataSource;
	
	public DBCPConnectionPool(){
		dataSource = new BasicDataSource();

		Config config = Config.getInstance();
		
		dataSource.setDriverClassName(config.getProperty(Constants.DRIVER_CLASS));
		dataSource.setUrl(config.getProperty(Constants.DB_URL));
		dataSource.setUsername(config.getProperty(Constants.USERNAME));
		dataSource.setPassword(config.getProperty(Constants.PASSWORD));
		dataSource.setPoolPreparedStatements(true);
	}
	
	public Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
	
	public void shutdown() {
		try {
			dataSource.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
